<?php
defined('IN_MAIN') or exit('Invalid Access!');

function prepare_prod_for_display_from_ssid($sid){
/*
This function prepare the $prods for formatting later, it gets the product information from the stockist sales detail table, using the input sales id $sid
and the output will be

$prods[index][product detail]
if product is a jumpstart package, then it will be
$prods[index][product detail]
$prods[index][pck_prod][index2][product detail]

product detail includes id, refno, title, pv, qty, prod_type (j/n) unescaped
*/

	global $vars;
	$db=$vars["db"];

	$t=@mysql_num_rows($r=mysql_query("select * from $db->stockist_sales_detail where slid='$sid' and pkid='0'"));
	//pkid = 0 means retrieve only product that is not sub-product of a package
	for($i=0;$i<$t;$i++){
		$this_prod=mysql_fetch_assoc($r);
		$dprods[$i]=$this_prod;
		$dprods[$i]["id"]=$this_prod["pid"];
		//package
		if($this_prod["prod_type"]=="p"){
			$t2=@mysql_num_rows($r2=mysql_query("select * from $db->stockist_sales_detail where slid='$sid' and pkid='$this_prod[pid]'"));
			for($j=0;$j<$t2;$j++){
				$this_pck_prod=mysql_fetch_assoc($r2);
				$dprods[$i]["pck_prod"][$j] = $this_pck_prod;
				$dprods[$i]["pck_prod"][$j]["id"] = $this_pck_prod["pid"];
			}
		}
	}

	return $dprods;
}
?>